import 'package:flutter/material.dart';
import 'package:flutter_pc_manager/app/components/button_small_widget.dart';
import 'package:flutter_pc_manager/app/modules/order_manager/models/order_manager_model.dart';
import 'package:flutter_pc_manager/app/modules/order_manager/views/order_search.dart';

import '../../../../config/GlobalColor.dart';

class OrderTable extends StatelessWidget {
  final List<OrderItem> records;

  final Function updateDishStatus;

  final Function showAddEditorFunc;

  final Function onSearchFunc;
  final Function onDeleteFunc;

  const OrderTable(
      {super.key,
      required this.records,
      required this.updateDishStatus,
      required this.showAddEditorFunc,
      required this.onSearchFunc,
      required this.onDeleteFunc});

  List<DataColumn> _getDataColumn() {
    return ['订单号','桌位号', '订单状态', '用户名', '手机号', '下单时间', '实收金额', '操作']
        .map(
          (label) => DataColumn(
            label: Expanded(
              flex: 1,
              child: Text(
                label,
                style: const TextStyle(fontSize: 16, color: Colors.black87),
              ),
            ),
          ),
        )
        .toList();
  }

  Widget _getOrderStatus(int status) {
    if (status == 1) {
      return const Text('待付款');
    }
    if (status == 2) {
      return const Text('待接单');
    }
    if (status == 3) {
      return const Text('做餐中');
    }
    if (status == 4) {
      return const Text('出餐中');
    }
    if (status == 5) {
      return const Text('已完成');
    }
    if (status == 6) {
      return const Text('已取消');
    }
    return const Text('退款');
  }

  Widget _getToDo(int status) {
    if (status == 2 || status == 3 || status == 4 || status == 7) {
      return Container(
        padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 10),
        decoration: BoxDecoration(
            color: Colors.red, borderRadius: BorderRadius.circular(6)),
        child: Text(
          status == 2
              ? '待接单'
              : status == 3
                  ? '待做餐'
                  : status == 4
                      ? '待出餐'
                      : '待退款',
          style: const TextStyle(color: Colors.white),
        ),
      );
    }

    return Container(
      padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 10),
      decoration: BoxDecoration(
          color: Colors.grey, borderRadius: BorderRadius.circular(6)),
      child: Text(
        status == 1
            ? '待付款'
            : status == 5
                ? '已完成'
                : '已取消',
        style: const TextStyle(color: Colors.white),
      ),
    );
  }

  List<DataRow> _getDataRow(List<OrderItem> records) {
    return records
        .asMap()
        .entries
        .map(
          (item) => DataRow(
            cells: [
              DataCell(Text(item.value.number ?? '')),
              DataCell(Text('${item.value.sitNum}')),
              // 订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消 7退款
              DataCell(_getOrderStatus(item.value.status)),
              DataCell(Text('${item.value.userName ?? '游客'}')),
              DataCell(Text(item.value.phone ?? '--')),
              DataCell(Text(item.value.orderTime ?? '')),
              DataCell(Text('${item.value.amount ?? 0.00}')),
              DataCell(_getToDo(item.value.status)),
            ],
          ),
        )
        .toList();
  }

  @override
  Widget build(BuildContext context) {
    return ListView(padding: const EdgeInsets.all(0), children: [
      const OrderSeach(),
      Container(
        margin: const EdgeInsets.only(top: 10),
        width: double.infinity,
        child: DataTable(
          border: TableBorder.symmetric(
              outside: const BorderSide(color: GlobalColor.bgColor)),
          columns: _getDataColumn(),
          rows: _getDataRow(records),
        ),
      )
    ]);
  }
}
